define(function(require, exports){
	var $		= require("jquery");
	var pubjs	= require('pubjs');
	var view	= require('@base/view');
	var util	= require('util');

	// 普通-消息列表
	var MessageList = view.container.extend({
		init:function(config){
			config = pubjs.conf(config, {
				"class": 'P-messageCenter'
			});

			this.Super('init', arguments);
		},
		afterBuild:function(){
			var self = this;
			var c = self.getConfig();

			// 构建主体
			self.append($([
				'<div class="M-formHead">',
					'<span class="M-formHeadTitle">'+LANG('消息公告')+'</span>',
					//'<span class="fr" pub-name="date" pub-mod="@base/common/date.dateRangeNew"/>',
				'</div>',
				'<div class="cl" pub-name="grid" pub-mod="admin/messageManage/grid/grid.messageGrid"/>'
			].join('')));

			var config = {
				'grid': {
					'hasSearch': true
				},
				'date': {
					'pos': 'bR'
				}
			};
			self.buildFromTemplate(config);

			// 侧边栏
			self.createAsync('sidebar', '@layout/v1/sidebar.base', {
				'target': c.targetSidebar,
				'height': 200,
				'items': [
					// 类型
					{'name': 'status', 'uri':'modules/sideBarBase.radio','config':{
						'title': LANG('类型：'),
						'data': [
							{ 'id': -1, 'name': LANG('所有') },
							{ 'id': 0, 'name': LANG('未读') },
							{ 'id': 1, 'name': LANG('已读') }
						],
						'value': -1
					}}
				]
			});
		},
		onOperateMenuShow: function(ev){
			var result = [];
			result.push({id: 'check', name: LANG('查看')});
			ev.returnValue = result;
			return false;
		},
		onOperateMenuSelect: function(ev){
			var op = ev.param[0];
			var data = ev.param[1];

			switch(op.key){
				case 'check':
					window.open('#user/message/check/'+data._id);
				break;
			}
			return false;
		},//响应右边侧边栏选项
		onSidebarFilter: function(ev) {
			var self = this
				,param = {};

			util.each(ev.param, function(item, idx){
				var type = idx;
				var id = item.id;
				switch (type) {
					case 'status':
						param.is_read = id;
					break;
				}
			});

			self.reload(param);

			return false;
		},
		// 搜索
		onSearch: function(ev) {
			this.reload({
				'word': ev.param
			});
			return false;
		},
		reload: function(param){
			this.$.grid.reload(param);
		}
	});
	exports.messageList = MessageList;

	// 产品管理员-消息列表
	var MessageManageList = view.container.extend({
		init:function(config){
			config = pubjs.conf(config, {
				"class": 'P-messageCenter'
			});

			this.Super('init', arguments);
		},
		afterBuild:function(){
			var self = this;
			var c = self.getConfig();

			// 构建主体
			self.append($([
				'<div class="M-formHead">',
					'<span class="M-formHeadTitle">'+LANG('后台消息公告')+'</span>',
					'<span class="ml10 addButton" pub-name="addButton" pub-mod="@base/common/drop.add"></span>',
					//'<span class="fr" pub-name="date" pub-mod="@base/common/date.dateRangeNew"/>',
				'</div>',
				'<div class="cl" pub-name="grid" pub-mod="admin/messageManage/grid/grid.messageManageGrid"/>'
			].join('')));

			var config = {
				'grid': {
					'hasSearch': true,
					'hasFilter': false
				},
				'date': {
					'pos': 'bR'
				},
				'addButton':{
					'def': LANG('发布公告'),
					'href': '#admin/messageManage/edit'
				}
			};
			self.buildFromTemplate(config);

			// 侧边栏
			self.createAsync('sidebar', '@layout/v1/sidebar.base', {
				'target': c.targetSidebar,
				'items': [

				]
			});
		},
		onOperateMenuShow: function(ev){
			var result = [];
			result.push({id: 'check', name: LANG('查看')});
			result.push({id: 'remove', name: LANG('删除')});
			ev.returnValue = result;
			return false;
		},
		onOperateMenuSelect: function(ev){
			var self = this;
			var op = ev.param[0];
			var data = ev.param[1];

			switch(op.key){
				case 'check':
					pubjs.controller.navigate('admin/messageManage/check/'+data._id);
				break;
				case 'remove':
					pubjs.confirm(LANG('真的要删除这条消息吗？'), function(isOk){
						if(isOk){
							pubjs.mc.send('message/remove', {'id': data._id}, self.onRemove.bind(self));
						}
					})

				break;
			}
			return false;
		},
		onRemove: function(err, data){
			if (err){
				pubjs.alert(err.message);
				return false;
			}
			pubjs.alert(LANG('删除成功！'));
			this.$.grid.reload();
		},
		onInputClick: function(ev){
			return false;
		},
		reload: function(param){
			this.$.grid.reload(param);
		}
	});
	exports.messageManageList = MessageManageList;

});